Ext.define('wmf.controller.billing.billing', {
    extend: 'Ext.app.Controller',
    stores: ['billing.billing',
             'billing.productNameOrId',
             'billing.product'
    ],
    models: ['billing.billing'],
    views: ['billing.main', 
            'billing.list',
            'billing.add',
            'billing.edit',
            'billing.query',
            'billing.east'],
    
    refs: [{
        ref: 'billingList',
        selector: 'billingList'
    },{
    	ref: 'billingMain',
        selector: 'billingMain'
    },{
        ref: 'billingQuery',
        selector: 'billingQuery'
    },{
        ref: 'billingAdd',
        selector: 'billingAdd'
    },{
        ref: 'billingEdit',
        selector: 'billingEdit'
	},{
        ref: 'billingEast',
        selector: 'billingEast'
    }],
    
    init: function () {
        this.control({
        	'billingList': {
        		beforerender : this.onBeforeRender,
        		celldblclick: this.onCellDblClick
            },
            'billingList button#add': {
                click: this.onAddButtonClick
            },
            'billingList button#query': {
                click: this.onQueryButtonClick
            },
            'billingList button#export': {
                click: this.onExportButtonClick
            },
			'billingEast tool#minimize': {
				click: this.onMinToolClick
			},
			'billingAdd button#save': {
				click: this.onAddSaveButtonClick
			},
			'billingAdd button#reset': {
				click: this.onAddResetButtonClick
			},
			'billingAdd button#cancel': {
				click: this.onMinToolClick
			},
			'billingEdit button#save': {
				click: this.onEditSaveButtonClick
			},
			'billingEdit button#reset': {
				click: this.onEditResetButtonClick
			},
			'billingEdit button#cancel': {
				click: this.onMinToolClick
			},
        	'billingQuery button#save': {
				click: this.onQuerySaveButtonClick
			},
			'billingQuery button#reset': {
				click: this.onQueryResetButtonClick
			},
			'billingQuery button#cancel': {
				click: this.onMinToolClick
			}
        });
        wmf.billing = {};
        wmf.billing.params = {};
        wmf.billing.data = {};
        wmf.billing.init = true;
    },

    onAddButtonClick: function(){
    	var win = this.getBillingEast();
        win.removeAll();
        win.add(Ext.widget("billingAdd"));
        win.show();
    },
    
    onCellDblClick: function(list, td, cellIndex, record, tr, rowIndex, e, eOpts){
    	var win = this.getBillingEast();
        win.removeAll();
        win.add(Ext.widget("billingEdit"));
        win.show();
        wmf.billing.data= record.data;
        if(record.data.billingOnSuccess == '1'){
        	wmf.billing.data.billingOnSuccess = true;
        }else{
        	wmf.billing.data.billingOnSuccess = false;
        }
        this.getBillingEdit().getForm().setValues(wmf.billing.data);
    },
    
    onQueryButtonClick: function(){
    	var win = this.getBillingEast();
        win.removeAll();
        win.add(Ext.widget("billingQuery"));
        win.show();
        this.getBillingQuery().getForm().setValues(wmf.billing.params);
    },
    
    onExportButtonClick: function(){
    	var productNameOrId = wmf.billing.params.productNameOrId ? wmf.billing.params.productNameOrId : '';
		var ecNameOrId = wmf.billing.params.ecNameOrId ? wmf.billing.params.ecNameOrId : '';
		window.location.href='billing/exportBillingList.do?productNameOrId='
			+encodeURI(productNameOrId)+'&ecNameOrId='
			+encodeURI(ecNameOrId);
    },

    onAddResetButtonClick: function(){
    	this.getBillingAdd().getForm().reset();
    },
    
	onAddSaveButtonClick: function(){
    	var me = this;
    	var former = this.getBillingAdd();
    	var value = former.getValues();
		var win = this.getBillingEast();
		Ext.Ajax.request({
			url:'billing/saveBilling.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getBillingList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onEditResetButtonClick: function(){
    	this.getBillingEdit().getForm().setValues(wmf.billing.data);
    },
    
	onEditSaveButtonClick: function(){
    	var me = this;
    	var former = this.getBillingEdit();
    	var value = former.getValues();
		var win = this.getBillingEast();
		Ext.Ajax.request({
			url:'billing/saveBilling.do',
			params: value,
			success : function(respond, opts) {
				var responds = Ext.decode(respond.responseText);
				if(responds.failure){
					Ext.Msg.alert("提示", responds.msg);
					return false;
				}
				win.hide();
				me.getBillingList().getStore().reload();
			},
			failure : function(respond, action) {
				Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
			}
		});
    },
    
    onQueryResetButtonClick: function(){
    	var form = this.getBillingQuery().getForm();
    	form.reset();
    },
    
	onQuerySaveButtonClick: function(){
    	var me = this;
    	var former = this.getBillingQuery();
    	var value = former.getValues();
		var win = this.getBillingEast();
		wmf.billing.init = true;
		wmf.billing.params = value;
		win.hide();
		me.getBillingList().getStore().pagebar.getStore().currentPage = 1;
		me.getBillingList().getStore().reload();
    },
	
	onMinToolClick: function(){
    	var win = this.getBillingEast();
    	win.hide();
    },
    
    onBeforeRender: function(component,options){
    	wmf.data.loadAction(this.getBillingMain().itemId, this.getBillingList());
    	wmf.billing.params = {};
    	component.getStore().load();
    }
});